<?php

namespace app\api\controller\merchant;

use app\api\model\Config;
use app\common\model\Merchant;
use app\common\controller\Api;
use app\common\model\Car as CarModel;
use app\common\model\CarCate;
use app\common\model\MerchantCar;
use function Sodium\add;

/**
 * 首页接口.
 */
class Store extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


	//店铺信息修改
	public function editmerchant(){
		$user = $this->auth->getUser();
		$params = $this->request->post();
		if (!isset($params['edit'])){
			$this->error('参数错误');
		}
		if ($params['edit'] == 1){
			$validate = new \app\common\validate\Merchant();
			if (!$validate->scene('apiedit')->check($params)) {
				$this->error($validate->getError());
			}
			$model = Merchant::get($user['id']);
			if(isset($params['address'])){
                $address = getAddress($params['address']);
                $params['province'] = $address['province'];
                $params['city'] = $address['city'];
                $params['area'] = $address['area'];
                $params['street'] = $address['street'];
            }
			if(isset($params['shop_banner'])){
			    $params['shop_banner'] = implode(',', $params['shop_banner']);
            }
			$result = $model->allowField([])->save($params);
			if ($result){
			    if(isset($params['longitude']) && isset($params['latitude'])){
                    MerchantCar::where(['merchant_id' => $user['id']])->update(['merchant_longitude' => $params['longitude'], 'merchant_latitude' => $params['latitude']]);
                }else{
                    MerchantCar::where(['merchant_id' => $user['id']])->update(['merchant_longitude' => $user['longitude'], 'merchant_latitude' => $user['latitude']]);
                }
				$this->success('保存成功');
			}else{
				$this->error('保存失败');
			}
		}else{
			$merchant = Merchant::field('id,shop_logo,shop_name,shop_banner,service,province,city,area,street,introduction,alipay_account,alipay_name,merchant_name,notice,longitude,latitude,open_time,night_time,signing_time,stay_time')->where(['status' => 1,'examine_status' => 2,'is_delete' => 1,'id' => $user['id']])->find();
			if (!$merchant){
				$this->error('店铺未找到');
			}else{
				$merchant = $merchant->toArray();
				$merchant['signing_time'] = $merchant['signing_time'] ? date('Y-m-d', $merchant['signing_time']) : '';
				$merchant['address'] = $merchant['province'] . $merchant['city'] . $merchant['area'] . $merchant['street'];
				unset($merchant['province'],$merchant['city'],$merchant['area'],$merchant['street']);
				$merchant['shop_banner'] = explode(',',$merchant['shop_banner']);
                $ruleList = Merchant::serviceList('',1);
                $newRuleList = [];
				if ($merchant['service']){
					$serviceArr = explode(',',$merchant['service']);
					$newService = [];
					$serviceList = Merchant::serviceList();
					foreach ($serviceArr as $key=>$val){
						$newService['value'][] = $serviceList[$val];
					}
					$flag = 0;
					foreach ($ruleList as $kk=>$vv){
						$newRuleList[$flag]['id'] = $vv['id'];
						$newRuleList[$flag]['value'] = $vv['value'];
						if(in_array($kk, $serviceArr)){
							$newRuleList[$flag]['checked'] = true;
						}else{
							$newRuleList[$flag]['checked'] = false;
						}
                        $flag++;
                    }
                    $newService['rule'] = $newRuleList;
				}else{
                    $flag = 0;
                    foreach ($ruleList as $kk=>$vv){
                        $newRuleList[$flag]['id'] = $vv['id'];
                        $newRuleList[$flag]['value'] = $vv['value'];
                        $newRuleList[$flag]['checked'] = false;
                        $flag++;
                    }
                    $newService['rule'] = $newRuleList;
                    $newService['value'] = [];
                }
				$merchant['service'] = $newService??[];
				$is_perfect = 1;
				if($merchant['alipay_account'] && $merchant['alipay_name']){
                    $is_perfect = 2;
                }
				$merchant['is_perfect'] = $is_perfect;
				$merchant['notice'] = Config::where('name', 'notice')->value('value');
				$this->success('获取成功',$merchant);
			}
		}

	}


}
